package com.orcs.leefei.utils.kotlin


/**
 * Created by FeiLee on 2016/12/26 11:16.
 */

import android.app.Fragment
import android.content.Context
import android.graphics.Typeface
import android.support.v7.widget.AppCompatDrawableManager
import android.util.TypedValue
import android.support.v4.app.Fragment as SupportFragment

inline fun Context.colorRes(colorResId: Int) =
        resources.getColor(colorResId)

inline fun Int.resToColor() =
        Orcs.resources.getColor(this)

inline fun Context.intRes(intResId: Int) =
        resources.getInteger(intResId)

inline fun Int.resToInt() =
        Orcs.resources.getInteger(this)

inline fun Context.stringRes(intResId: Int) =
        resources.getString(intResId)

inline fun Int.resToString() =
        Orcs.resources.getString(this)

inline fun Context.dimenRes(dimenResId: Int) =
        resources.getDimensionPixelSize(dimenResId)

inline fun Int.resToPx() =
        Orcs.resources.getDimensionPixelSize(this)

inline fun Context.stringArrayRes(arrayResId: Int) =
        resources.getStringArray(arrayResId)

inline fun Int.toStringArray(arrayResId: Int) =
        Orcs.resources.getStringArray(arrayResId)

inline fun Context.intArrayRes(arrayResId: Int) =
        resources.getIntArray(arrayResId)

inline fun Int.toIntArray(arrayResId: Int) =
        Orcs.resources.getIntArray(arrayResId)

inline fun Context.drawableRes(drawableResId: Int) =
        AppCompatDrawableManager.get().getDrawable(this, drawableResId)

inline fun Int.resToDrawable() =
        AppCompatDrawableManager.get().getDrawable(Orcs.context, this)

inline fun Context.typefaceFromAssets(assetPathResId: Int) =
        typefaceFromAssets(getString(assetPathResId))

inline fun Int.toTypefaceFromAssets(assetPathResId: Int) =
        Orcs.resources.getString(assetPathResId).typefaceFromAssets()

inline fun Context.typefaceFromAssets(assetPath: String) =
        Typeface.createFromAsset(assets, assetPath)

inline fun String.typefaceFromAssets() =
        Typeface.createFromAsset(Orcs.context.assets, this)

inline fun Fragment.colorRes(colorResId: Int) =
        resources.getColor(colorResId)

inline fun Fragment.intRes(intResId: Int) =
        resources.getInteger(intResId)

inline fun Fragment.dimenRes(dimenResId: Int) =
        resources.getDimensionPixelSize(dimenResId)

inline fun Fragment.stringArrayRes(arrayResId: Int) =
        resources.getStringArray(arrayResId)

inline fun Fragment.intArrayRes(arrayResId: Int) =
        resources.getIntArray(arrayResId)

inline fun Fragment.drawableRes(drawableResId: Int) =
        AppCompatDrawableManager.get().getDrawable(context, drawableResId)

inline fun Fragment.typefaceFromAssets(assetPathResId: Int) =
        typefaceFromAssets(getString(assetPathResId))

inline fun Fragment.typefaceFromAssets(assetPath: String) =
        Typeface.createFromAsset(activity.assets, assetPath)

inline fun SupportFragment.colorRes(colorResId: Int) =
        resources.getColor(colorResId)

inline fun SupportFragment.intRes(intResId: Int) =
        resources.getInteger(intResId)

inline fun SupportFragment.dimenRes(dimenResId: Int) =
        resources.getDimensionPixelSize(dimenResId)

inline fun SupportFragment.stringArrayRes(arrayResId: Int) =
        resources.getStringArray(arrayResId)

inline fun SupportFragment.stringRes(stringResId: Int) =
        resources.getString(stringResId)

inline fun SupportFragment.stringRes(stringResId: Int, vararg formatArgs: Any) =
        resources.getString(stringResId, *formatArgs)

inline fun SupportFragment.intArrayRes(arrayResId: Int) =
        resources.getIntArray(arrayResId)

inline fun SupportFragment.drawableRes(drawableResId: Int) =
        AppCompatDrawableManager.get().getDrawable(context, drawableResId)

inline fun SupportFragment.typefaceFromAssets(assetPathResId: Int) =
        typefaceFromAssets(getString(assetPathResId))

inline fun SupportFragment.typefaceFromAssets(assetPath: String) =
        Typeface.createFromAsset(activity.assets, assetPath)

inline fun Int.toPx(context: Context = Orcs.context): Int =
        TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                this.toFloat(),
                context.resources.displayMetrics
        ).toInt()

inline fun Float.toPx(context: Context = Orcs.context): Float =
        TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                this,
                context.resources.displayMetrics
        )

inline fun Int.toDp(context: Context = Orcs.context): Int {
    return Math.ceil((this / context.resources.displayMetrics.density).toDouble()).toInt()
}

inline fun Float.toDp(context: Context = Orcs.context): Float {
    return Math.ceil((this / context.resources.displayMetrics.density).toDouble()).toFloat()
}

